using System;
using System.IO;
using System.Xml.Serialization;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Data.Domain;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using StructureMap;

namespace MathematicallySafe.HeadCounter.Data.Models
{
    [Serializable]
    public class UserOptionsModel : IUserOptions
    {
        private bool _isDirty;
        private UserOptions _userOptions;

        public UserOptionsModel()
        {
            if (File.Exists(Constants.OptionsFileName))
            {
                Load();
            }
            else
            {
                CreateDefault();
            }
        }

        private void CreateDefault()
        {
            _userOptions = new UserOptions()
                               {
                                   HeadCounterPath = @"C:\World of Warcraft\Interface\AddOns\Broker_HeadCounter"
                               };
        }

        public string HeadCounterPath
        {
            get { return _userOptions.HeadCounterPath; }
            set { _userOptions.HeadCounterPath = value; }
        }

        public int MaxBackups
        {
            get { return _userOptions.MaxBackups; }
            set { _userOptions.MaxBackups = value; }
        }

        public string FTPDomain
        {
            get { return _userOptions.FTPDomain; }
            set { _userOptions.FTPDomain = value; }
        }

        public string FTPPath
        {
            get { return _userOptions.FTPPath; }
            set { _userOptions.FTPPath = value; }
        }

        public string FTPUserName
        {
            get { return _userOptions.FTPUserName; }
            set { _userOptions.FTPUserName = value; }
        }

        public string FTPPassword
        {
            get { return _userOptions.FTPPassword; }
            set { _userOptions.FTPPassword = value; }
        }

        public bool IsDirty
        {
            get { return _isDirty; }
        }

        public bool IsClean
        {
            get { return !_isDirty; }
        }

        public void SetClean()
        {
            _isDirty = false;
        }

        public void SetDirty()
        {
            _isDirty = true;
        }

        private void Load()
        {
            _userOptions = SerialisationHelper.Load<UserOptions>(Constants.UserOptionsFileName);
        }

        public void Save()
        {
            SerialisationHelper.Save(Constants.UserOptionsFileName, _userOptions);
        }

    }
}